package com.sen.gateway.util;

import cn.hutool.core.convert.Convert;
import com.sen.common.common.inter.RespCodeEnum;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.Charset;
/**
 * @author esjiang
 */
@Slf4j
public class WebfluxResponseUtils {

    /**
     * webflux的response返回json对象
     */
    public static Mono<Void> responseWriter(ServerWebExchange exchange, MessageResult messageResult) {
        return responseWrite(exchange, messageResult);
    }

    public static Mono<Void> responseWrite(ServerWebExchange exchange, MessageResult result) {
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().setAccessControlAllowCredentials(true);
        response.getHeaders().setAccessControlAllowOrigin("*");
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        try {
            DataBuffer buffer = dataBufferFactory.wrap(JacksonUtils.toJSONString(result).getBytes(Charset.forName("utf-8")));
            return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
                DataBufferUtils.release(buffer);
            });
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null ;
    }

    public static Mono<Void> responseWrite(ServerWebExchange exchange, OAuth2Error result) {
        ServerHttpResponse response = exchange.getResponse();
        response.getHeaders().setAccessControlAllowCredentials(true);
        response.getHeaders().setAccessControlAllowOrigin("*");
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        DataBufferFactory dataBufferFactory = response.bufferFactory();
        MessageResult messageResult = MessageResult.reqFailed(new RespCodeEnum() {
            @Override
            public int getCode() {
                return Convert.toInt(result.getErrorCode());
            }

            @Override
            public String getMsg() {
                return result.getDescription();
            }

            @Override
            public boolean isSuccess() {
                return false;
            }
        });
        try {
            DataBuffer buffer = dataBufferFactory.wrap(JacksonUtils.toJSONString(messageResult).getBytes(Charset.forName("utf-8")));
            return response.writeWith(Mono.just(buffer)).doOnError((error) -> {
                DataBufferUtils.release(buffer);
            });
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return null ;
    }
}
